package com.zbxx.service.impl;

import com.zbxx.dao.DepartmentDao;
import com.zbxx.domain.Department;
import com.zbxx.domain.PageBean;
import com.zbxx.service.DepartmentService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//部门管理的业务层的实现类
/*事务管理*/
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
    //注入dao
    @Resource
    private DepartmentDao departmentDao;

    public DepartmentDao getDepartmentDao() {
        return departmentDao;
    }

    public void setDepartmentDao(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    //分页查询部分的方法
    @Override
    public PageBean<Department> findByPage(Integer currPage) {
        PageBean<Department> pageBean=new PageBean<Department>();
        //封装当前页面
        pageBean.setCurrPage(currPage);
        //封装每页显示记录数
        int pageSize=3;
        pageBean.setPageSize(pageSize);
        //封装总记录数
        int totalCount =departmentDao.findCount();
        pageBean.setTotalCount(totalCount);
        double tc=totalCount;
        Double num= Math.ceil(tc/pageSize);
        pageBean.setTotalPage(num.intValue());
        //封装每页显示数据
        int begin=(currPage-1)*pageSize;
        List<Department> list= departmentDao.findByPage(begin,pageSize);
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    //业务层保存部门的方法
    public void save(Department department) {
        departmentDao.save(department);
    }

    @Override
    //根据部门的id去查询部门的方法
    public Department findById(Integer did) {
        return departmentDao.findById(did);
    }
    //业务层修改部门的方法
    @Override
    public void update(Department department) {
        departmentDao.update(department);
    }
    //业务层删除部门的方法
    @Override
    public void delete(Department department) {
        departmentDao.delete(department);
    }

    //查询所有部门的方法
    @Override
    public List<Department> findAll() {
        return departmentDao.findAll();
    }


}
